﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace OfflineReport
{
    public partial class frmBaoCaoThuNo : Form
    {
        string str_cnn = ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
        int type = 0;
        SQL sql = new SQL();
        private string amount = "";
        private string typePR = "PR";
        private string typeIN = "IN";
        private string typePE = "PE";
        private string typePS = "PS";

        string store_pr = "ThuNo";
       

        public frmBaoCaoThuNo()
        {
            InitializeComponent();
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            type = getLHKH(cbLdKh.Text.Trim());
            type = getLHKH(cbLdKh.Text.Trim());
            SqlParameter[] prALL = new SqlParameter[8];
            prALL[0] = new SqlParameter("@CURRENCY", cbCurr.Text.Trim());
            prALL[1] = new SqlParameter("@LoaiKH", type);
            prALL[2] = new SqlParameter("@LDLINK", tbLD_link.Text.Trim());
            prALL[3] = new SqlParameter("@MaKH", tbIDkh.Text.Trim());
            prALL[4] = new SqlParameter("@dateFrom", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
            prALL[5] = new SqlParameter("@dateTo", dateTimePicker2.Value.ToString("MM/dd/yyyy"));
            prALL[6] = new SqlParameter("@Officer", cb_officer.Text.Trim());
            prALL[7] = new SqlParameter("@Type", amount);

            SqlParameter[] prPR = new SqlParameter[8];
            prPR[7] = new SqlParameter("@Type", typePR);
            for (int i = 0; i < 7; i++)
            {
                prPR[i] = prALL[i];
            }

            SqlParameter[] prIN = new SqlParameter[8];
            prIN[7] = new SqlParameter("@Type", typeIN);
            for (int i = 0; i < 7; i++)
            {
                prIN[i] = prALL[i];
            }

            SqlParameter[] prPE = new SqlParameter[8];
            prPE[7] = new SqlParameter("@Type", typePE);
            for (int i = 0; i < 7; i++)
            {
                prPE[i] = prALL[i];
            }
            SqlParameter[] prPS = new SqlParameter[8];
            prPS[7] = new SqlParameter("@Type", typePS);
            for (int i = 0; i < 7; i++)
            {
                prPS[i] = prALL[i];
            }


            DataTable dtALL = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dtALL, prALL);

            DataTable dtPR = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dtPR, prPR);

            DataTable dtIN = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dtIN, prIN);

            DataTable dtPE = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dtPE, prPE);

            DataTable dtPS = new DataTable();
            SqlHelper.ExecuteDataTable(str_cnn, CommandType.StoredProcedure, store_pr, dtPS, prPS);
            dtALL.Columns.Add("PR", typeof(Double));
            dtALL.Columns.Add("IN", typeof(Double));
            dtALL.Columns.Add("PS", typeof(Double));
            dtALL.Columns.Add("PE", typeof(Double));


            for (int i = 0; i < dtALL.Rows.Count; i++)
                for (int j = 0; j < dtPR.Rows.Count; j++)
                {
                    if ((dtALL.Rows[i]["REPORT_DATE"].ToString() == dtPR.Rows[j]["REPORT_DATE"].ToString()) && (dtALL.Rows[i]["LD_ID"].ToString() == dtPR.Rows[j]["LD_ID"].ToString()))
                    {
                        dtALL.Rows[i]["PR"] = dtPR.Rows[j]["PR"];
                    }
                }
            for (int i = 0; i < dtALL.Rows.Count; i++)
                for (int j = 0; j < dtIN.Rows.Count; j++)
                {
                    if ((dtALL.Rows[i]["REPORT_DATE"].ToString() == dtIN.Rows[j]["REPORT_DATE"].ToString()) && (dtALL.Rows[i]["LD_ID"].ToString() == dtIN.Rows[j]["LD_ID"].ToString()))
                    {
                        dtALL.Rows[i]["IN"] = dtIN.Rows[j]["IN"];
                    }
                }
            for (int i = 0; i < dtALL.Rows.Count; i++)
                for (int j = 0; j < dtPE.Rows.Count; j++)
                {
                    if ((dtALL.Rows[i]["REPORT_DATE"].ToString() == dtPE.Rows[j]["REPORT_DATE"].ToString()) && (dtALL.Rows[i]["LD_ID"].ToString() == dtPE.Rows[j]["LD_ID"].ToString()))
                    {
                        dtALL.Rows[i]["PE"] = dtPE.Rows[j]["PE"];
                    }
                }
            for (int i = 0; i < dtALL.Rows.Count; i++)
                for (int j = 0; j < dtPS.Rows.Count; j++)
                {
                    if ((dtALL.Rows[i]["REPORT_DATE"].ToString() == dtPS.Rows[j]["REPORT_DATE"].ToString()) && (dtALL.Rows[i]["LD_ID"].ToString() == dtPS.Rows[j]["LD_ID"].ToString()))
                    {
                        dtALL.Rows[i]["PS"] = dtPS.Rows[j]["PS"];
                    }
                }
            for (int i = 0; i < dtALL.Rows.Count; i++)
            {
                if (dtALL.Rows[i]["PR"].ToString() == "") dtALL.Rows[i]["PR"] = 0;
                if (dtALL.Rows[i]["IN"].ToString() == "") dtALL.Rows[i]["IN"] = 0;
                if (dtALL.Rows[i]["PE"].ToString() == "") dtALL.Rows[i]["PE"] = 0;
                if (dtALL.Rows[i]["PS"].ToString() == "") dtALL.Rows[i]["PS"] = 0;
            }
            Data ds1 = new Data();
            for (int i = 0; i < dtALL.Rows.Count; i++)
            {
                ds1.Tables["DT"].Rows.Add(dtALL.Rows[i]["ID_KH"], dtALL.Rows[i]["NAMEKH"], dtALL.Rows[i]["TYPE_KH"], dtALL.Rows[i]["N_TYPEKH"], dtALL.Rows[i]["REPORT_DATE"], dtALL.Rows[i]["LD_ID"], dtALL.Rows[i]["CURRENCY"], dtALL.Rows[i]["LD_LINK"], dtALL.Rows[i]["PR"], dtALL.Rows[i]["IN"], dtALL.Rows[i]["PE"], dtALL.Rows[i]["PS"], dtALL.Rows[i]["OFFICER"]);
            }

            OfflinReport rp = new OfflinReport();
            rp.SetDataSource(ds1);

            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
            crParameterDiscreteValue.Value = dateTimePicker1.Value.ToString("MM/dd/yyyy");
            crParameterFieldDefinitions = rp.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["dateFrom"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;
            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            ParameterFieldDefinitions crParameterFieldDefinitions1;
            ParameterFieldDefinition crParameterFieldDefinition1;
            ParameterValues crParameterValues1 = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();
            crParameterDiscreteValue1.Value = dateTimePicker2.Value.ToString("MM/dd/yyyy");
            crParameterFieldDefinitions1 = rp.DataDefinition.ParameterFields;
            crParameterFieldDefinition1 = crParameterFieldDefinitions1["dateTo"];
            crParameterValues1 = crParameterFieldDefinition1.CurrentValues;
            crParameterValues1.Clear();
            crParameterValues1.Add(crParameterDiscreteValue1);
            crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


            crystalReportViewer1.ReportSource = rp;
            crystalReportViewer1.Refresh();
           
           
        }

        private int getLHKH(string type_kh)
        {
            int n = 0;
            switch (type_kh)
            {
                case "All":
                    n=0;
                    break;
                case "Cá nhân":
                    n = 1;
                    break;
                case "Doanh nghiệp":
                    n = 2;
                    break;
                default:
                    n = 0;
                    break;
                    
            }
            return n;
        }

        private void frmBaoCaoThuNo_Load(object sender, EventArgs e)
        {
        }
    }
}
